package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.SupplierDistribution;
import com.scs.application.modules.base.mapper.SupplierDistributionMapper;
import com.scs.application.modules.base.request.SupplierDistributionQueryRequest;
import com.scs.application.modules.base.service.SupplierDistributionService;
import com.scs.application.modules.base.vo.SupplierDistributionVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 供应商授权配送 service 实现
 *
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class SupplierDistributionServiceImpl extends BaseServiceImpl<SupplierDistributionMapper, SupplierDistribution> implements SupplierDistributionService {

    @Override
    public List<SupplierDistribution> list(SupplierDistributionQueryRequest request) {
        List<SupplierDistribution> list = this.list(getQueryWrapper(request));
        return list;
    }

    @Override
    public IPage<SupplierDistribution> page(SupplierDistributionQueryRequest request) {
        IPage page = this.page(request.getPage(), getQueryWrapper(request));
        return page;
    }

    @Override
    public IPage<SupplierDistributionVO> pageAuthorizeSupplier(SupplierDistributionQueryRequest request) {
        request.setSupplierId(UserUtils.getSupplierId());
        IPage page = this.baseMapper.pageAuthorizeSupplier(request.getPage(), request);
        return page;
    }

    @Override
    public IPage<SupplierDistributionVO> pageAuthorize(SupplierDistributionQueryRequest request) {
        request.setSupplierId(UserUtils.getSupplierId());
        IPage page = this.baseMapper.pageAuthorize(request.getPage(), request);
        return page;
    }

    private QueryWrapper<SupplierDistribution> getQueryWrapper(SupplierDistributionQueryRequest request){
        return Wrappers.<SupplierDistribution>query()
                .orderByDesc("gmt_modified");
    }
}
